<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="aId">
                    <el-input v-model="formData.aId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="caseId">
                    <el-input v-model="formData.caseId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="userId">
                    <el-input v-model="formData.userId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="订单编号" prop="outTradeNo">
                    <el-input v-model="formData.outTradeNo" placeholder="请输入订单编号" />
                </el-form-item>
                <el-form-item label="1-律师支付 2-案件扫码支付" prop="type">
                    <el-select class="flex-1" v-model="formData.type" placeholder="请选择1-律师支付 2-案件扫码支付">
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="1-微信支付 2-余额支付 3-支付宝支付" prop="payType">
                    <el-select class="flex-1" v-model="formData.payType" placeholder="请选择1-微信支付 2-余额支付 3-支付宝支付">
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="" prop="amount">
                    <el-input v-model="formData.amount" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="平台分佣收入" prop="fyAmount">
                    <el-input v-model="formData.fyAmount" placeholder="请输入平台分佣收入" />
                </el-form-item>
                <el-form-item label="地区公司分佣收入" prop="areasFyAmount">
                    <el-input v-model="formData.areasFyAmount" placeholder="请输入地区公司分佣收入" />
                </el-form-item>
                <el-form-item label="律师分佣收入" prop="lawyerFyAmount">
                    <el-input v-model="formData.lawyerFyAmount" placeholder="请输入律师分佣收入" />
                </el-form-item>
                <el-form-item label="合伙人分佣" prop="partnerFyAmount">
                    <el-input v-model="formData.partnerFyAmount" placeholder="请输入合伙人分佣" />
                </el-form-item>
                <el-form-item label="" prop="payTime">
                    <el-input v-model="formData.payTime" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="1-已支付" prop="status">
                    <el-radio-group v-model="formData.status" placeholder="请选择1-已支付">
                        <el-radio label="0">请选择字典生成</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="" prop="transactionId">
                    <el-input v-model="formData.transactionId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="prepayId">
                    <el-input v-model="formData.prepayId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="newlandTradeNo">
                    <el-input v-model="formData.newlandTradeNo" placeholder="请输入" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  orderEdit, orderAdd, orderDetail } from '@/api/order'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑案件订单' : '新增案件订单'
})

const formData = reactive({
    id: '',
    aId: '',
    caseId: '',
    userId: '',
    outTradeNo: '',
    type: '',
    payType: '',
    amount: '',
    fyAmount: '',
    areasFyAmount: '',
    lawyerFyAmount: '',
    partnerFyAmount: '',
    payTime: '',
    status: '',
    transactionId: '',
    prepayId: '',
    newlandTradeNo: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    aId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    caseId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    userId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    outTradeNo: [
        {
            required: true,
            message: '请输入订单编号',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择1-律师支付 2-案件扫码支付',
            trigger: ['blur']
        }
    ],
    payType: [
        {
            required: true,
            message: '请选择1-微信支付 2-余额支付 3-支付宝支付',
            trigger: ['blur']
        }
    ],
    amount: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    fyAmount: [
        {
            required: true,
            message: '请输入平台分佣收入',
            trigger: ['blur']
        }
    ],
    areasFyAmount: [
        {
            required: true,
            message: '请输入地区公司分佣收入',
            trigger: ['blur']
        }
    ],
    lawyerFyAmount: [
        {
            required: true,
            message: '请输入律师分佣收入',
            trigger: ['blur']
        }
    ],
    partnerFyAmount: [
        {
            required: true,
            message: '请输入合伙人分佣',
            trigger: ['blur']
        }
    ],
    payTime: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择1-已支付',
            trigger: ['blur']
        }
    ],
    transactionId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    prepayId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    newlandTradeNo: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await orderEdit(data) : await orderAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await orderDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
